package my.vo.voImpl;


import my.vo.Vo;

import java.io.BufferedReader;
import java.io.FileWriter;

/**
 * @author 星环
 * @date 2022/10/31 9:43:01
 * @description
 */
public class BookType extends Vo {
    private String name;

    public BookType(){
    }

    public BookType(String name) {
        this.name = name;
    }

    public BookType(int id, String name) {
        this.id = id;
        this.name = name;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "BookType{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public void saveBy(FileWriter fileWriter) {
        StringBuilder sb = new StringBuilder();
        sb.append(id).append(" ").append(name).append("\n");
        try {
            fileWriter.write(sb.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean readBy(BufferedReader fileReader) {
        try {
            String s = fileReader.readLine();
            if(s == null){
                return false;
            }
            String[] fields = s.split(" ");
            if(fields.length != 2) return false;
            int id = Integer.parseInt(fields[0]);
             setId(id);
             setName(fields[1]);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Vo getEmptyInstance() {
        return new BookType();
    }
}
